#include <stdlib.h>
#include <assert.h>
#include <err.h>

#include "edge.h"

struct sEdge{
    node n;             /* Destino de la arsita */
    unsigned int cap;   /* Capacidad de la arista */
    unsigned int flow;  /* Flujo transportado por la arista */
};

edge edge_create(const node n, const unsigned int cap){
    edge e = NULL;

    assert(n != NULL);

    /* Asigno memoria para alojar la estructura */
    e = (edge) malloc(sizeof(struct sEdge));
    if (e == NULL)
        return e;/* Si no hay mem devuelvo NULL */

    /* Inicializo la estructura */
    e->n = n;
    e->cap = cap;
    e->flow = 0;

    assert(e != NULL);
    assert(e->n != NULL);

    return e;
}

edge edge_destroy(edge e){
    assert(e != NULL);
    assert(e->n != NULL);

    /* El nodo se libera desde otro lado. */
    e->n = NULL;

    free(e);
    e = NULL;

    return e;
}

node edge_getNeighbor(const edge e){
    assert (e != NULL);
    return e->n;
}

unsigned int edge_getCap(const edge e){
    assert(e != NULL);
    return e->cap;
}

unsigned int edge_getFlow(const edge e){
    assert(e != NULL);
    return e->flow;
}

void edge_increaseFlow(edge e, unsigned int flow){
    assert(e != NULL);
    assert(e->flow + flow <= e->cap);

    e->flow = e->flow + flow;
}

void edge_decreaseFlow(edge e, unsigned int flow){
    assert(e != NULL);

    e->flow = e->flow - flow;
}

bool edge_isSaturated(const edge e){
    assert(e != NULL);
    return (e->flow == e->cap);
}

bool edge_isEmpty(const edge e){
    assert(e != NULL);
    return (e->flow == 0);
}

